package com.fan.admin.util;

import com.fan.admin.service.CollegeInfoService;
import com.fan.admin.service.SystemInfoService;
import com.fan.commons.pojo.CollegeInfo;
import com.fan.commons.pojo.SystemInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 信息的本地缓存
 *
 * @author fan
 * @date 2021/2/24
 */
@Component
@Slf4j
public class LocalCache {

	@Autowired
	private CollegeInfoService collegeInfoService;

	@Autowired
	private SystemInfoService systemInfoService;

	private static Map<String, CollegeInfo> collegeCache = new ConcurrentHashMap<>();
	private static Map<String, SystemInfo> systemCache = new ConcurrentHashMap<>();

	/**
	 * spring 生命周期方法
	 */
	@PostConstruct
	public void init() {
		List<CollegeInfo> collegeInfos = collegeInfoService.queryCollegeInfo();
		for (CollegeInfo collegeInfo : collegeInfos) {
			collegeCache.put(collegeInfo.getCollegeId(), collegeInfo);
		}
		List<SystemInfo> systemInfoList = systemInfoService.querySystemInfo(null);
		for (SystemInfo systemInfo : systemInfoList) {
			systemCache.put(systemInfo.getSystemId(), systemInfo);
		}
		log.info("init {}", "college and system success!");
	}

	public CollegeInfo getCollegeInfoCache(String collegeId) {
		return collegeCache.get(collegeId);
	}

	public SystemInfo getSystemInfoCache(String systemId) {
		return systemCache.get(systemId);
	}
}
